package com.whatisjee.ssf.domain.dao.impl;

import static org.apache.commons.lang.StringUtils.isBlank;

import java.util.Iterator;
import java.util.List;

import com.whatisjee.ssf.domain.dao.HelperDao;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import javax.persistence.Query;

@Named("helperDao")
@ApplicationScoped
public class HelperDaoImpl extends AbstractDaoImpl implements HelperDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<String> selectDistincts(final String entityName, final String propName) {
		String hql = "SELECT DISTINCT(a." + propName + ") FROM " + entityName + " a";
                Query query = em.createQuery(hql);
		List<String> list = (List<String>) query.getResultList();
		for(Iterator<String> iter = list.iterator(); iter.hasNext();) {
			String str = iter.next();
			if(isBlank(str)) {
				iter.remove();
			}
		}
		return list;
	}

	@Override
	public void flush() {
		em.flush();
	}

	@Override
	public void initialize(Object object) {
//		em.initialize(object); // TODO: find the equivalent method
	}

	@Override
	public void save(Object entity) {
		em.persist(entity);
	}

	@Override
	public void delete(Object entity) {
		em.remove(entity);
	}

	@Override
	public void evict(Object entity) {
		em.detach(entity);
	}

}
